//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Logging Application Block
//===============================================================================
// License: http://www.codeplex.com/entlibcontrib/Project/License.aspx
// Author: Randy
//===============================================================================

using System;
using System.Globalization;

namespace EntLibContrib.Logging.Parser
{
	/// <summary>
	/// Encapsulates the logic for parsing the time stamp text from a log file.
	/// </summary>
	public class TimeStampParserFormat : ITimeStampParser
	{
		#region Fields
		private string timeStampFormat;
		private IFormatProvider formatProvider;
		#endregion

		#region Properties
		/// <summary>
		/// Gets or sets the time stamp format.
		/// </summary>
		/// <value>The time stamp format.</value>
		public string TimeStampFormat
		{
			get { return timeStampFormat; }
			set { timeStampFormat = value; }
		}

		/// <summary>
		/// Specify time stamp format for month-day or day-month order, etc.
		/// </summary>
		/// <value>The format provider.</value>
		public IFormatProvider FormatProvider
		{
			get { return formatProvider; }
			set { formatProvider = value; }
		}
		#endregion

		#region Construction
		/// <summary>
		/// Constructor to initialize a new instance of the class.
		/// </summary>
		/// <param name="timeStampFormat">time stamp format for month-day or day-month order, etc.</param>
		public TimeStampParserFormat(string timeStampFormat)
		{
			this.timeStampFormat = timeStampFormat;
			this.formatProvider = CultureInfo.CurrentCulture;
		}

		/// <summary>
		/// Constructor to initialize a new instance of the class.
		/// </summary>
		/// <param name="timeStampFormat">time stamp format for month-day or day-month order, etc.</param>
		/// <param name="formatProvider">time stamp format provider for month-day or day-month order, etc.</param>
		public TimeStampParserFormat(string timeStampFormat, IFormatProvider formatProvider)
		{
			this.timeStampFormat = timeStampFormat;
			this.formatProvider = formatProvider;
		}
		#endregion

		#region Public Methods
		/// <summary>
		/// Parse the time stamp text from a log file.
		/// </summary>
		/// <param name="timeStampText">time stamp text</param>
		/// <returns>date and time</returns>
		public DateTime ParseTimeStamp(string timeStampText)
		{
			return DateTime.ParseExact(timeStampText, timeStampFormat, formatProvider);
		}
		#endregion
	}
}
